package tool;
import java.io.*;
import java.util.Properties;
 
public class God{
    private God(){}
 
    /**
     * @param path 属性配置文件的路径
     * @param key 键
     * @return 键对应的值
     */
    public static String getValue(String path, String key) {
        Properties props = new Properties();
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
        try (InputStreamReader isr = new InputStreamReader(is, "UTF-8");) {//请用UTF格式保存.properties文件。
            props.load(isr);//从流中加载properties文件信息
        } catch (UnsupportedEncodingException ex) {
        } catch (IOException ex) {
        }
        return props.getProperty(key);
    }
    
    //简化版本，path固定为项目根目录下的my.properties
    public static Object create(String key) {
        return create("my.properties", key);
    }
 
    public static Object create(String path, String key) {
        String typeName = getValue(path, key);
        Object obj = null;
        if (typeName != null) {
            try {
                obj = Class.forName(typeName).newInstance();
            }catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            }
        }
        return obj;
    }
}
